package org.eesgmbh.gimv.samples.googlechartapi.client;

import org.eesgmbh.gimv.client.controls.DragImageControl;
import org.eesgmbh.gimv.client.controls.MouseWheelControl;
import org.eesgmbh.gimv.client.event.StateChangeEvent;
import org.eesgmbh.gimv.client.presenter.CalendarPresenter;
import org.eesgmbh.gimv.client.presenter.ImagePresenter;
import org.eesgmbh.gimv.client.presenter.ZoomBoxPresenter;
import org.eesgmbh.gimv.client.view.GenericWidgetView;
import org.eesgmbh.gimv.client.view.GenericWidgetViewImpl;
import org.eesgmbh.gimv.client.view.ImageViewImpl;
import org.eesgmbh.gimv.samples.googlechartapi.client.calendar.CustomCalendarViewImpl;
import org.eesgmbh.gimv.samples.googlechartapi.client.infobox.InfoBoxPresenter;
import org.eesgmbh.gimv.samples.googlechartapi.client.infobox.InfoBoxViewImpl;
import org.eesgmbh.gimv.samples.googlechartapi.client.linecolors.ColorListPresenter;
import org.eesgmbh.gimv.samples.googlechartapi.client.linecolors.ColorListViewImpl;
import org.eesgmbh.gimv.shared.util.Bound;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.ui.RootPanel;

public class GoogleChartApiSampleEntryPoint implements EntryPoint {

	public void onModuleLoad() {
		/*
		 * DeferredCommand is a workaround for GWT-Bug in Java 5, Windows, hosted mode in conjunction with UIBinder
		 * http://code.google.com/p/google-web-toolkit/issues/detail?id=4254
		 * Error: java.lang.VerifyError: (class: com/google/gwt/core/client/Scheduler, method: <init> signature: ()V) Illegal constant pool index
		 */
		DeferredCommand.addCommand(new Command() {
			public void execute() {
				HandlerManager handlerManager = new HandlerManager(null);

				//Initialize and bind the layout
				GoogleChartApiSampleLayout layout = new GoogleChartApiSampleLayout();

				//Instantiate the controller
				GoogleChartApiSampleController appController = new GoogleChartApiSampleController(layout.getViewport(), handlerManager);

				//set up Gimv components
				configureGimvComponents(handlerManager, layout);

				//add the layout to the rootPanel div defined in GoogleChartApiSample.html
				RootPanel.get("rootPanel").add(layout.getRootWidget());

				//Initialize Gimv components to interpret a drag (holding the left mouse button
				//and moving the mouse) as a move of the image within the viewport
				//alternativly this could be setup for zooming into the image with a bounding box
				handlerManager.fireEvent(StateChangeEvent.createMove());

				appController.init();
			}
		});
	}

	private void configureGimvComponents(HandlerManager handlerManager, GoogleChartApiSampleLayout layout) {

		/*
		 * Configures the views, presenters and controls of Gimv
		 */

		//Out of the box components

		ImagePresenter.View imageView = new ImageViewImpl(layout.getChartImage());
		new ImagePresenter(handlerManager, imageView);

		new DragImageControl(handlerManager);
		new MouseWheelControl(handlerManager);

		GenericWidgetView zoomBoxView = new GenericWidgetViewImpl(layout.getZoomBox());
		new ZoomBoxPresenter(handlerManager, zoomBoxView);

		//Customized components

		InfoBoxPresenter.View infoBoxView = new InfoBoxViewImpl(layout.getUrlLink(), layout.getMousePositionLabel());
		new InfoBoxPresenter(handlerManager, infoBoxView);

		CalendarPresenter.View startCalendarView = new CustomCalendarViewImpl(layout.getStartDatePicker(), layout.getCalendarStartDateLabel(), layout.getStartCalendarImage());
		CalendarPresenter startCalendarPresenter = new CalendarPresenter(handlerManager, startCalendarView);
		startCalendarPresenter.configureBound(Bound.LEFT);

		CalendarPresenter.View endCalendarView = new CustomCalendarViewImpl(layout.getEndDatePicker(), layout.getCalendarEndDateLabel(), layout.getEndCalendarImage());
		CalendarPresenter endCalendarPresenter = new CalendarPresenter(handlerManager, endCalendarView);
		endCalendarPresenter.configureBound(Bound.RIGHT);

		ColorListPresenter.View colorListView1 = new ColorListViewImpl(layout.getColorsLine1ListBox());
		colorListView1.setColor(0);
		ColorListPresenter colorListPresenter1 = new ColorListPresenter(handlerManager, colorListView1);
		colorListPresenter1.setIndex(1);

		ColorListPresenter.View colorListView2 = new ColorListViewImpl(layout.getColorsLine2ListBox());
		colorListView2.setColor(1);
		ColorListPresenter colorListPresenter2 = new ColorListPresenter(handlerManager, colorListView2);
		colorListPresenter2.setIndex(2);

		layout.getViewport().setHandlerManager(handlerManager);
		layout.getViewport().setEnableZoomWhenShiftkeyPressed(true);
	}
}
